package com.qixin.api.serviceimpl;

import com.qixin.api.bean.ComCar;
import com.qixin.api.bean.ComUserCarRenting;
import com.qixin.api.bean.Tuizu;
import com.qixin.api.bean.pojo.WelcomScroll;
import com.qixin.api.mapper.ComUserCarRentingMapper;
import com.qixin.api.service.ComCarService;
import com.qixin.api.service.ComUserCarRentingService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.qixin.api.service.TuizuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.SQLException;
import java.util.List;

/**
 * <p>
 * 用户租车中间表 服务实现类
 * </p>
 *
 * @author 吕正睿
 * @since 2018-11-12
 */
@Service
public class ComUserCarRentingServiceImpl extends ServiceImpl<ComUserCarRentingMapper, ComUserCarRenting> implements ComUserCarRentingService {
	@Autowired
	ComCarService comCarService;

	@Autowired
	TuizuService tuizuService;

	@Override
	public Integer updaterefresh(Long datetime) throws SQLException {
		baseMapper.updaterefresh(datetime);
		return null;
	}

	@Override
	public String rentcar(ComUserCarRenting comUserCarRenting) {
		ComCar comCar = comCarService.findCarByCarId(comUserCarRenting.getCarId());
		if (comUserCarRenting.getNumber() < comCar.getNum()) {
			if (this.insert(comUserCarRenting)) {
				Integer a = baseMapper.updategroudinga(comUserCarRenting.getCarId(), comUserCarRenting.getNumber());
				if (a == null) {
					return "租车成功";
				}
				return "下架失败";
			}
		}

		if (comCar.getNum() == comUserCarRenting.getNumber().intValue()) {
			if (this.insert(comUserCarRenting)) {
				Integer b = baseMapper.updategrouding(comUserCarRenting.getCarId());
				if (b == null) {
					return "租车成功";
				}
				return "下架失败";
			}
		}


//		if (this.insert(comUserCarRenting)){
//			Integer a=baseMapper.updategrouding(comUserCarRenting.getCarId());
//        if (a==null){
//			return "租车成功";
//		}
//		return "下架失败";
//		}

		return "请输入正确驻车数量";
	}

	@Override
	public List<WelcomScroll> findWelcomScroll(Long id) {
		return baseMapper.findWelcomScroll(id);
	}

	@Override
//	@Transactional(rollbackFor = {SQLException.class,Exception.class})
	public boolean comit(Long carId, Long dataId, Long userId) {
		Tuizu tuizu = new Tuizu();
		tuizu.setExamine(1);
		tuizu.setId(dataId);
		if (tuizuService.updateById(tuizu)) {
			baseMapper.delectcarid(carId, userId);
				return true;

		}
//		throw  new SQLException();
		return  false;
	}



}
